package com.zdj;

import java.util.Arrays;

/**
 * @author zhangdj
 * @date 2021-10-12 17:21:39
 * 截取一定位数的字符串，求剩下字符串的最小整数值
 */
public class GetMinimumString {
    public static void main(String[] args) {
        System.out.println(cutString("143002019", 3));
    }

    /**
     * 截取一定位数字符串 获取剩下字符串的最小值
     *
     * @param s     字符串
     * @param count 截取位数
     * @return 最小值
     */
    public static String cutString(String s, int count) {
        int length = s.length();
        if (length <= 0) {
            return s;
        }
        if (count >= length) {
            return "";
        }
        // 截取指定位数字符串
        String substring = s.substring(0, count);
        String stringAfter = s.replaceAll(substring, "");
        char[] chars = stringAfter.toCharArray();
        // 转为整形数组
        int l = chars.length;
        int[] arr = new int[l];
        for (int i = 0; i < l; i++) {
            int numericValue = Character.getNumericValue(chars[i]);
            arr[i] = numericValue;
        }
        // 剩余升序排序
        for (int i = 0; i < l; i++) {
            for (int j = 0; j < l - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        // 如果第一位是0则和后面第一位不是0的数字交换
        if (arr[0] == 0) {
            for (int i = 1; i < l; i++) {
                if (arr[i] != 0) {
                    int temp;
                    temp = arr[0];
                    arr[0] = arr[i];
                    arr[i] = temp;
                    break;
                }
            }
        }
        return Arrays.toString(arr);
    }
}
